//========================================================================
//Copyright 2007-2011 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package com.baby;

import java.util.ArrayList;
import java.util.Collection;

import com.baby.schema.Schema;


public abstract class CollectionFactory
{
    
    public static final String FIELD_NAME_VALUE = "v";
    
    static final int ARRAY_BLOCKING_QUEUE_INITIAL_SIZE = 
        Integer.getInteger("protostuff.collectionschema.array_block_queue_initial_size", 
                16);
    
    /**
     * Creates new {@code Collection} messages.
     */
    public interface MessageFactory
    {
        
        /**
         * Creates a new {@code Collection} message.
         */
        public <V> Collection<V> newMessage();
    }
    
    public enum MessageFactories implements MessageFactory
    {
        // defaults to ArrayList
        Collection
        {
            public <V> Collection<V> newMessage()
            {
                return new ArrayList<V>();
            }
        },
        // defaults to ArrayList
        List
        {
            public <V> Collection<V> newMessage()
            {
                return new ArrayList<V>();
            }
        },
        ArrayList
        {
            public <V> Collection<V> newMessage()
            {
                return new ArrayList<V>();
            }
        },
        LinkedList
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.LinkedList<V>();
            }
        },
        CopyOnWriteArrayList
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.CopyOnWriteArrayList<V>();
            }
        },
        Stack
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.Stack<V>();
            }
        },
        Vector
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.Vector<V>();
            }
        },
        // defaults to HashSet
        Set
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.HashSet<V>();
            }
        },
        HashSet
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.HashSet<V>();
            }
        },
        LinkedHashSet
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.LinkedHashSet<V>();
            }
        },
        // defaults to TreeSet
        SortedSet
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.TreeSet<V>();
            }
        },
        // defaults to TreeSet
        NavigableSet
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.TreeSet<V>();
            }
        },
        TreeSet
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.TreeSet<V>();
            }
        },
        ConcurrentSkipListSet
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.ConcurrentSkipListSet<V>();
            }
        },
        CopyOnWriteArraySet
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.CopyOnWriteArraySet<V>();
            }
        },
        // defaults to LinkedList
        Queue
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.LinkedList<V>();
            }
        },
        // defaults to LinkedBlockingQueue
        BlockingQueue
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.LinkedBlockingQueue<V>();
            }
        },
        LinkedBlockingQueue
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.LinkedBlockingQueue<V>();
            }
        },
        // defaults to LinkedList
        Deque
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.LinkedList<V>();
            }
        },
        // defaults to LinkedBlockingDeque
        BlockingDeque
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.LinkedBlockingDeque<V>();
            }
        },
        LinkedBlockingDeque
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.LinkedBlockingDeque<V>();
            }
        },
        ArrayBlockingQueue
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.ArrayBlockingQueue<V>(
                        ARRAY_BLOCKING_QUEUE_INITIAL_SIZE);
            }
        },
        ArrayDeque
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.ArrayDeque<V>();
            }
        },
        ConcurrentLinkedQueue
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.ConcurrentLinkedQueue<V>();
            }
        },
        PriorityBlockingQueue
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.concurrent.PriorityBlockingQueue<V>();
            }
        },
        PriorityQueue
        {
            public <V> Collection<V> newMessage()
            {
                return new java.util.PriorityQueue<V>();
            }
        };
        
        /**
         * Returns the message factory for the standard jdk {@link Collection} 
         * implementations.
         */
        public static MessageFactories getFactory(Class<? extends Collection<?>> clazz)
        {
            return clazz.getName().startsWith("java.util") ? 
                    MessageFactories.valueOf(clazz.getSimpleName()) : null;
        }
        
        /**
         * Returns the message factory for the standard jdk {@link Collection} 
         * implementations.
         */
        public static MessageFactories getFactory(String name)
        {
            return MessageFactories.valueOf(name);
        }
    }
    
    /**
     * Factory for creating {@link Collection} messages.
     */
    public final MessageFactory messageFactory;
    
    public CollectionFactory()
    {
        this(MessageFactories.ArrayList);
    }
    
    public CollectionFactory(MessageFactory messageFactory)
    {
        this.messageFactory = messageFactory;
    }
    
   

    public final String messageFullName()
    {
        return Collection.class.getName();
    }

    public final String messageName()
    {
        return Collection.class.getSimpleName();
    }
    
    public final Class<? super Collection> typeClass()
    {
        return Collection.class;
    }
    
    public final Collection newMessage()
    {
        return messageFactory.newMessage();
    }

   
    
 

}
